class Solution
{
private:
    void square(int &x)
    {
        int ans = 0;
        while (x)
        {
            ans += (x % 10) * (x % 10);
            x /= 10;
        }
        x = ans;
    }

public:
    // 两种方法：哈希表，快慢指针
    bool isHappy(int n)
    {
        if (n == 1)
            return true;
        int fast = n, low = n;
        do
        {
            square(fast), square(fast);
            square(low);
            if (low == 1)
                return true;
        } while (fast != low);
        return false;
    }
};